import { DocumentObject, Types } from '@amcax/base';
import { RendererAssembly, RendererMgr } from '@amcax/renderer';
import { IAssemblyDocTreeNodeCAD } from '../types';
import { ctx2 } from '@app-base/module';

/**
 * 处理装配-平面节点
 */
const addPlane2GeomBox = (
  part: DocumentObject,
  parentDoc: IAssemblyDocTreeNodeCAD,
) => {
  const planeNode = createPlaneNode(part, parentDoc);
  parentDoc.children.push(planeNode);
  const isVisible = part.visibility && parentDoc.visible;
  ctx2.dataManager.addObjectsInfo({
    ...part,
    isRecursiveVisible: isVisible,
  });

  const renderer = RendererMgr.getRender<RendererAssembly>('assembly');
  ctx2.referenceManager.removeObjects([part.dxid]);
  renderer.renderReferenceObject(
    {
      ...part,
      children: [],
      isRecursiveVisible: isVisible,
    },
    part.dxid,
  );
};

function createPlaneNode(
  rootObject: DocumentObject,
  docTreeRoot: IAssemblyDocTreeNodeCAD,
): IAssemblyDocTreeNodeCAD {
  const planeNode: IAssemblyDocTreeNodeCAD = {
    key: rootObject.dxid,
    title: rootObject.label || '平面',
    type: Types.PLANE,
    parent: docTreeRoot,
    visible: rootObject.visibility,
    originalObj: rootObject,
    recursivelyVisible: true,
    collectionUUID: docTreeRoot.collectionUUID,
    version: 0,
    documentInfo: docTreeRoot.documentInfo,
  };
  return planeNode;
}

export { addPlane2GeomBox };
